<?php echo $this->Html->script('jquery-1.4.2.min.js', false); ?>
<?php echo $this->Html->script('timer.js', false); ?>
<?php echo $this->Html->script('jquery.nap.js', false); ?>

<h2>Worklog</h2>
<?php if($worklog): ?>
<div class='box'>
    <?php echo $form->create("Worklog",array("action"=>"addLog", 'id'=>'formLog')); ?>
    <div class='grid_10 alpha'>
    <?php echo $form->textarea("Worklog.log",array('id'=>'logField',"rows"=>1, "style"=>"width:100%;height:24px; font-size:1.5em;padding:4px;"));?>
    </div>
    <div class='grid_2 omega'>
    <div class="buttons">
        <?php
            echo $ajax->submit("Add",array(
                "url"=>"addLog",
                 "id"=>"btnUpdate",
                 "class"=>"button",
                 'style' =>'width:100%',
                 "div"=>false,
                 "before"=>"$('#btnUpdate').addClass('disabled');$('#WorklogLog').removeClass('highlight');$('#WorklogLog').addClass('disabled')",
                 "complete"=>"$('WorklogLog').focus();$('#btnUpdate').removeClass('disabled');$('#WorklogLog').removeClass('disabled');$('#WorklogLog').addClass('highlight');$('#log_item').prepend(request.responseText);timerLog.restart();"
            ));
        ?>
    </div>
    </div>
    <div class='clear'></div>
    <?php echo $form->end(); ?>
</div>
<div class='grid_12 alpha omega'>
        <table cellpadding="0" cellspacing="0" class="list oddeven" id="log">
		<thead>
        <tr>
            <th width='80px'><?php __('Log');?></th>
            <th >&nbsp;</th>
            <th class="actions">&nbsp;</th>
        </tr>
		</thead>
		<tbody id="log_item">
        <?php foreach ($worklog['Log'] as $log):?>
            <tr>
                <td>
					<span style='font-size:11px;color:#888;'><?php echo $time->niceShort($log['created']); ?></span>
                </td>
                <td class='log'>
					<?php echo $log['content']; ?>
                </td>
                <td class="actions">
                    <?php echo $this->Html->link(__('Delete', true), array('action'=>'log_delete', $log['id']), null, sprintf(__('Are you sure you want to delete # %s?', true), $log['id'])); ?>
                </td>
            </tr>
        <?php endforeach; ?>
		</tbody>

        </table>
</div>
<?php else: ?>
            <p class='box'>Klik tombol dibawah untuk memulai perhitungan waktu kerja Anda.</p>
            <div class="buttons">
               <?php echo $this->Html->link(" Start ",array("action"=>"startwork"), array("class"=>"button", 'style'=>'width:50%; text-align:center;font-size:1.4em;padding:8px'), false, false); ?>
           </div>
<?php endif; ?>
</div>

<script>
    var timerLog = new Timer();
    var timerUpdateActivity = new Timer();

    var INTERVAL_LOG = <?php echo $interval['log'] ?> * 1000;
    var INTERVAL_UPDATE_ACTIVITY = <?php echo $interval['updateActivity'] ?> * 1000;

    var activityMode = 'active'; // or 'idle'

    timerUpdateActivity.interval(INTERVAL_UPDATE_ACTIVITY)
    .addCallback(updateActivity)
    .start();

    function updateActivity()
    {
        if (window.activityMode == 'active') {
            // send ajax to server to update active time
            $.ajax({
               type: "POST",
               url: "<?php echo $this->Html->url('/worklogs/updateActiveTime') ?>",
               success: function(msg){
                    $('#activeTime').html(msg);
               }
             });
        // kode di bawah ini tidak akan pernah dieksekusi selama prompt window masih dipakai. todo: ganti native prompt menjadi custom dialog box
        } else if(window.activityMode == 'idle') {
            // you are idle
            $.ajax({
               type: "POST",
               url: "<?php echo $this->Html->url('/worklogs/idle') ?>",
             });
        }
    }

    timerLog.interval(INTERVAL_LOG)
    .addCallback(askLog)
    .start();

    function askLog()
    {
        updateActivity();
        timerLog.stop();
        activityMode = 'idle';
        var currentLog = prompt('Apa yang sedang Anda lakukan saat ini?', $('#logField').val());
        if(currentLog){
            //update and submit form
            $('#logField').val(currentLog);
            $('#btnUpdate').triggerHandler('click');
            lastLog = currentLog;
        }
        timerLog.start();
        activityMode = 'active';
    }

    function alertLog()
    {
        
    }
    //$(document).ready(function() {
    //    $(document).nap(
    //        // idle function
    //        function() {
    //            $('#status').html('idle');
    //            timerUpdateActiveTime.stop();
    //        },
    //        // wake up function
    //        function() {
    //            $('#status').html('active');
    //            timerUpdateActiveTime.start();
    //        },
    //        // idle time, in seconds
    //        INTERVAL_PRE_IDLE
    //    );
    //});
</script>
